//创建最近阅读的控制器JS
msqbapp.controller('topicitemController',function ($scope,msqbService) {
    $scope.userId =0;
    $scope.topicId =0;
    $scope.title="";
    $scope.imgsrc="img/小星星.png";
    $scope.storebean={userId:0,topicId:0,storeTime:""};
    $scope.commentBean={userId:0,topicId:0,commentContent:"",commentTime:""};
    //最近阅读的初始化函数
    $scope.topicitemInit = function () {
            var arr = window.location.search.split('&');
            for (var i = 0;i<arr.length;i++){
                var strarr = arr[i].split('=');
                if(strarr[0]=='?userId'){
                    $scope.userId = strarr[1];
                    msqbService.findOne($scope.userId) .success(function (resp) {
                        $scope.userBean = resp;
                    })
                }
                if(strarr[0]=='topicId'){
                    $scope.topicId = strarr[1];
                }
                if(strarr[0]=='title'){
                    $scope.title = decodeURIComponent(strarr[1]);
                }
            }
            //读取具体题目对象
        msqbService.getTopicItem($scope.userId,$scope.topicId).success(function (resp) {
                //题目对象
                $scope.getTopic = resp;
                //选项对象getTopicObject
                $scope.getTopicObject=JSON.parse($scope.getTopic.topicOption);
        });
            //查看具体题目是否被收藏
        msqbService.isShouCang($scope.userId,$scope.topicId).success(function (resp) {
            if (resp.success) {
                //如果resp是true，表示该题被该用户收藏过
                $scope.imgsrc="img/小星星2.png";
            }
        });
            //读取具体题目的评论列表
        msqbService.getCommentBeanList($scope.topicId).success(function (resp) {
            $scope.commentBeanList=resp.rows;
        })
    };
    //得到具体题目答案
    $scope.getAnswer=function (topicId) {
        msqbService.getAnswer(topicId).success(function (resp) {
            //答案对象
            $scope.answerObject=resp;
            //相关知识点对象xiangguanObject
            $scope.xiangguanObject=JSON.parse($scope.answerObject.answerRelative);
        })
    };
    //修改阅读状态
    $scope.updateReadLogStatus=function (userId,topicId,readLogStatus) {
        msqbService.updateReadLogStatus(userId,topicId,readLogStatus).success(function (resp) {
        });
    };
    //发布评论信息的函数
    $scope.addComment=function () {
        if ($scope.commentBean.commentContent==''||$scope.commentBean.commentContent.length==0) {
            alert("评论内容不能为空")
        }else {
            $scope.commentBean.userId=$scope.userId;
            $scope.commentBean.topicId=$scope.topicId;
            msqbService.addComment($scope.commentBean).success(function (resp) {
                if (resp.success){
                    //读取具体题目的评论列表
                    msqbService.getCommentBeanList($scope.topicId).success(function (resp) {
                        $scope.commentBeanList=resp.rows;
                    });
                    $scope.commentBean.commentContent='';
                }
            })
        }
    };

    $scope.testShouCang=function () {
        if ($scope.imgsrc=="img/小星星.png") {
            $scope.storebean.userId=$scope.userId;
            $scope.storebean.topicId=$scope.topicId;
            //发送收藏请求
            msqbService.addStore($scope.storebean).success(function (resp) {
                alert("pppp");
                if (resp.success) {
                    alert(resp.message);
                    $scope.imgsrc="img/小星星2.png"
                }
            });
        }else{
            //发送删除请求
            //发送收藏请求
            msqbService.changeStore($scope.userId,$scope.topicId).success(function (resp) {
                alert("sssssss");
                if (resp.success) {
                    alert(resp.message);
                    $scope.imgsrc="img/小星星.png"
                }
            });
        }
    }
});
